%module array
%{
#include "array.hpp"
%}

typedef unsigned int uint;
typedef unsigned char byte;

template <class T>
class array {
	public:
	array(size_t n, size_t m=1);
	array(size_t n, void * d);
	array(size_t n, size_t m, void * d);
	array(const array& bl);
  array& operator=(const array& b);
	~array(); 
	T get(size_t i, size_t j=0) const;
	T set(size_t i, T v);
	T set(size_t i, size_t j, T v);
  T * data() const;
	void copy(const array& b);
	void times(const array& x, array& y) const;
	void times(const array& x);
	void sum(const array& x, array& y) const;
	void sum(const array& x);
	void zero();
	size_t size() const;
	size_t width() const;
	size_t dim() const;
	size_t rows() const;
	size_t columns() const;
};

%template(array_double) array<double>;
%template(array_float) array<float>;
%template(array_int) array<int>;
%template(array_uint) array<uint>;
%template(array_char) array<char>;
%template(array_byte) array<byte>;

%luacode {
  local atype={"double","float","int","uint","char", "byte"}
  local new=function (stype) 
    return function (t,u)
      local b
      if type(t)=="table" then
        b=array["array_"..stype](#t)
        for i=1,#t do b:set(i-1,t[i]) end
      else
        if u then 
          b=array["array_"..stype](t,u)
        else 
          b=array["array_"..stype](t)
        end
      end
      return b
    end
  end
  for i=1,#atype do
    local stype=atype[i]
    array[stype]=new(stype)
  end

  --print "module array loaded"
}

